using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class GUICharacterPresetLoader : MonoBehaviour
{
	public RectTransform CharacterPresetPrefab;

	public Transform Canvas;

	public RectTransform View;

	public Text Description;

	public bool ChangeCharacter;

	private void Start()
	{
		Draw();
	}

	public void GetCharacterPrefab()
	{
	}

	private void ClearCanvas()
	{
		if (!(Canvas == null))
		{
			IEnumerator enumerator = Canvas.transform.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Transform transform = (Transform)enumerator.Current;
					UnityEngine.Object.Destroy(transform.gameObject);
				}
			}
			finally
			{
				IDisposable disposable;
				if ((disposable = (enumerator as IDisposable)) != null)
				{
					disposable.Dispose();
				}
			}
		}
	}

	public void Draw()
	{
		if (Canvas == null || CharacterPresetPrefab == null || View == null || !(UnitZ.levelManager != null))
		{
			return;
		}
		ClearCanvas();
		float num = 0f;
		RectTransform component = Canvas.gameObject.GetComponent<RectTransform>();
		Vector2 sizeDelta = CharacterPresetPrefab.sizeDelta;
		float num2 = (sizeDelta.x + 5f) * (float)UnitZ.characterManager.CharacterPresets.Length;
		if (num2 < View.rect.width)
		{
			num = (View.rect.width - num2) / 2f;
		}
		for (int i = 0; i < UnitZ.characterManager.CharacterPresets.Length; i++)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(CharacterPresetPrefab.gameObject, Vector3.zero, Quaternion.identity);
			gameObject.transform.SetParent(Canvas.transform);
			GUICharacterPreset component2 = gameObject.GetComponent<GUICharacterPreset>();
			if ((bool)component2)
			{
				component2.GUIImage.texture = UnitZ.characterManager.CharacterPresets[i].Icon;
				component2.Name.text = UnitZ.characterManager.CharacterPresets[i].Name;
				component2.Description = UnitZ.characterManager.CharacterPresets[i].Description;
				component2.Index = i;
				component2.changeCharacter = ChangeCharacter;
			}
			RectTransform component3 = gameObject.GetComponent<RectTransform>();
			if ((bool)component3)
			{
				RectTransform rectTransform = component3;
				float num3 = num;
				Vector2 sizeDelta2 = CharacterPresetPrefab.sizeDelta;
				float num4 = num3 + (sizeDelta2.x + 5f) * (float)i;
				Vector2 sizeDelta3 = CharacterPresetPrefab.sizeDelta;
				rectTransform.anchoredPosition = new Vector2(num4 + sizeDelta3.x / 2f, 5f);
				component3.localScale = CharacterPresetPrefab.gameObject.transform.localScale;
			}
		}
		RectTransform rectTransform2 = component;
		float num5 = num + num;
		Vector2 sizeDelta4 = CharacterPresetPrefab.sizeDelta;
		float x = num5 + (sizeDelta4.x + 5f) * (float)UnitZ.characterManager.CharacterPresets.Length;
		Vector2 sizeDelta5 = component.sizeDelta;
		rectTransform2.sizeDelta = new Vector2(x, sizeDelta5.y);
	}
}
